package com.aruba.attendance.ui.widget.calendar

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable

/**
 * Created by aruba on 2023/3/9.
 */
@Composable
fun rememberSelectDateState(
    redirectToCurrent: Boolean = false,
    selectDate: String
): SelectDateState {
    return rememberSaveable(saver = SelectDateState.Saver) {
        SelectDateState(mutableStateOf(redirectToCurrent), mutableStateOf(selectDate))
    }
}

class SelectDateState(
    var startRedirectToCurrentPage: MutableState<Boolean>,
    var selectDate: MutableState<String>,
) {
    fun scrollToDate(date: String) {
        selectDate.value = date
        startRedirectToCurrentPage.value = true
    }

    companion object {
        val Saver: Saver<SelectDateState, *> = listSaver(
            save = { listOf(it.startRedirectToCurrentPage.value, it.selectDate.value) },
            restore = {
                SelectDateState(
                    startRedirectToCurrentPage = mutableStateOf(it[0] as Boolean),
                    selectDate = mutableStateOf(it[1] as String)
                )
            }
        )
    }
}